package com.ljwy.gun;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ljwy.gun.bean.CpInfo;
import com.ljwy.gun.okgoHelper.GsonConvert;
import com.ljwy.gun.okgoHelper.HttpRequestHelper;
import com.ljwy.gun.okgoHelper.JsonCallback;
import com.ljwy.gun.utils.cache.ACacheHelper;
import com.lzy.okgo.model.Response;
import com.rscja.deviceapi.RFIDWithUHFBLE;
import com.rscja.deviceapi.interfaces.ScanBTCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ljwy.gun.Constant.GET_CP_LIST3;
import static com.ljwy.gun.Constant.IP;

public class CpListActivity extends BaseActivity{
    // private BluetoothAdapter mBtAdapter;
    private TextView mEmptyList;
    public static final String TAG = "DeviceListActivity";
    private TextView tvTitle;
    private CpInfo info;
    private CpListAdapter cpListAdapter;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        // 设置窗体背景透明
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        setContentView(R.layout.device_list);

//        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
//            Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
//            finish();
//        }

        init();
        getCpLit();
    }

    private void getCpLit() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("activity_id",ACacheHelper.getInstance().getString("ACID",""));
        HttpRequestHelper.getRequest(this, IP + GET_CP_LIST3, hashMap, false, new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                System.out.println(response.body());
                info = GsonConvert.fromJson(response.body(), CpInfo.class);
                cpListAdapter = new CpListAdapter(CpListActivity.this,info.getData());
                ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
                newDevicesListView.setAdapter(cpListAdapter);
                newDevicesListView.setOnItemClickListener(mDeviceClickListener);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
            }
        });
    }

    private void init() {
        tvTitle = findViewById(R.id.title_devices);
        tvTitle.setText("选择cp打卡点");
        mEmptyList = (TextView) findViewById(R.id.empty);
        mEmptyList.setVisibility(View.GONE);

        Button btnClearHistory = findViewById(R.id.btnClearHistory);
        btnClearHistory.setVisibility(View.GONE);
        findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        Button cancelButton = (Button) findViewById(R.id.btn_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


    private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CpInfo.DataBean data = info.getData().get(position);
            System.out.println(data.toString());
            ACacheHelper.getInstance().putString("CPID",String.valueOf(data.getId()));
            Intent result = new Intent();
            result.putExtra("name",data.getName());
            setResult(205, result);
            finish();

        }
    };

    class CpListAdapter extends BaseAdapter {
        Context context;
        List<CpInfo.DataBean> cpList;
        LayoutInflater inflater;

        public CpListAdapter(Context context, List<CpInfo.DataBean> cpList) {
            this.context = context;
            inflater = LayoutInflater.from(context);
            this.cpList = cpList;
        }

        @Override
        public int getCount() {
            return cpList.size();
        }

        @Override
        public Object getItem(int position) {
            return cpList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup vg;

            if (convertView != null) {
                vg = (ViewGroup) convertView;
            } else {
                vg = (ViewGroup) inflater.inflate(R.layout.device_element, null);
            }

            CpInfo.DataBean cp = cpList.get(position);
            final TextView tvadd = ((TextView) vg.findViewById(R.id.address));
            final TextView tvname = ((TextView) vg.findViewById(R.id.name));
            final TextView tvpaired = (TextView) vg.findViewById(R.id.paired);
            final TextView tvrssi = (TextView) vg.findViewById(R.id.rssi);

            tvpaired.setVisibility(View.GONE);
            tvrssi.setVisibility(View.GONE);

            tvname.setText(cp.getName());
            tvname.setTextColor(Color.BLACK);
            tvadd.setTextColor(Color.BLACK);
            tvadd.setText(String.valueOf(cp.getId()));
            return vg;
        }
    }
}
